<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<u-search 
				shape="square"
				:show-action="true" 
				action-text="搜索" 
				:clearabled="true"  
				:animation="true" 
				v-model="keywords"
				:disabled="true"
				@click="goSearch"
			></u-search>
		</view>
		<view class="container" :style="{'height':cateHeight+'px'}">
				<!-- left左侧一级分类列表 -->
				<scroll-view scroll-y class="left">
					<!-- 循环遍历的 -->
					<view class="left_list" 
						v-for="(item,index) in catelist" 
						:key="item.id" 
					>
						<text class="activeIndex" >{{item.catename}}</text>
					</view>
				</scroll-view>
				<!-- right右侧二级分类详情 -->
				<scroll-view scroll-y  class="right" :scroll-into-view="id">			
					<view class="right_box" :id="'id'+index" v-for="(item,index) in catelist" :key="item.id">
						<view class="row_title">{{item.catename}}</view>
						<view class="row">
							<view 
								class="row_list" 
								v-for="(m,i) in item.children" 
								:key="m.id" 
								@click="toGoodList(m.id)"
							>	
								<image :src="m.img" mode="widthFix"></image>
								<text>{{m.catename}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return { 
				id:'id0',//设置右侧默认置顶分类id
				activeIndex:0,//默认左侧分类选中下标
				cateHeight:'',//初始化分类容器高度
				catelist:[],//初始化分类数据
				keywords:''
			}
		},
		onLoad() {
			// 动态获取当前设备的高度
			this.cateHeight = uni.getSystemInfoSync().windowHeight;
			// 发起所有分类的请求
			this.requestCates();
		},
			
		methods:{
			// 跳转到搜索页面
			goSearch(){
				uni.navigateTo({
					url:'/pages/search/search'
				})
			},
			// 发起所有分类的请求
			requestCates(){
				this.$http({url:'/api/getcates'}).then(res=>{
					if(res.data.code === 200){
						// 处理图片路径
						res.data.list.forEach(item=>{
							if(item.children){
								item.children.forEach(i=>{
									i.img = this.$baseUrl + i.img
								})
							}
						})
						this.catelist = res.data.list;
					}
				})
			},
			// 跳转商品列表
			toGoodList(cateid){
				// 路由跳转
				uni.navigateTo({
					url:`/pages/goodlist/goodlist?cateid=${cateid}&type=2`
				})
			}
		}
	
	}
</script>

<style lang="scss" scoped>
/* 引入样式 */
	@import url("@/static/css/cate.css");
</style>
